package com.example.iqiyitest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.iqiyi.movieksdk.sdk.exported.CodecType;
import com.iqiyi.movieksdk.sdk.exported.OnPlayStateChangeListener;
import com.iqiyi.movieksdk.sdk.exported.PlayerContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;


public class PlayerActivity extends Activity implements OnPlayStateChangeListener {


    private static final String TAG ="PlayerActivity" ;
    private TestView testView;
    private PlayerContract.Presenter playController;
    private ProgressBar progressBar;
    private long position;
    private EditText viewById;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        long  qipuId = getIntent().getLongExtra("qipuId", 0l);
        int  playType = getIntent().getIntExtra("playType", 0);
        testView = new TestView(this,qipuId,playType,this);//0，大播放 1，new player
         progressBar = (ProgressBar) testView.findViewById(R.id.progress);
        setContentView(testView);
        testView.onCreate();
        playController = testView.getPlayImp();
        playController.doSetCodecType(CodecType.SYSTEM);
        playController.doSetPlayerSize(true);
        viewById = (EditText) findViewById(R.id.edit_progress);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        testView.onDestory();
    }
    public void changeSize(View v){
        playController.doSetPlayerSize(new Random().nextInt(1000),new Random().nextInt(1000));
    }
    public void seekTo(View v){
        String s = viewById.getText().toString();
        int i = Integer.parseInt(s);
        String movieInfo = getMovieInfo();
        try {
            JSONObject jsonObject=new JSONObject(movieInfo);
            long movieTotalTime = jsonObject.getLong("movieTotalTime");

            playController.doSeek((long) (i/100f*movieTotalTime));
            Log.d(TAG,"movieTotalTime="+movieTotalTime+"doseek "+i/100*movieTotalTime);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public void play(View v){
        playController.doStart();
    }
    public void pause(View v){
        playController.doPlayOrPause(false);
    }
    public void resume(View v){
        playController.doPlayOrPause(true);
    }
    public void seek(View v){
        playController.doSeek(1000*60);
    }
    public void stop(View v){
        playController.doStop();
    }
    public void getMovieInfo(View v){
        String movieInfo = getMovieInfo();
        TextView textView = (TextView) testView.findViewById(R.id.tv);
        textView.setText(movieInfo);
        Log.d(TAG,movieInfo);
    }
    public String getMovieInfo(){
        return playController.doGetMovieInfo();
    }
    @Override
    public void showPlayEnd() {
        Toast.makeText(this, "showPlayEnd", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showPlayBegin() {
        Toast.makeText(this, "showPlayBegin", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {
        Toast.makeText(this, "showLoading", Toast.LENGTH_SHORT).show();
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void showLoadingComplete() {
        Toast.makeText(this, "showLoadingComplete", Toast.LENGTH_SHORT).show();
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showPause() {
          Toast.makeText(this, "showPause", Toast.LENGTH_SHORT).show();
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void showPlaying() {
        Toast.makeText(this, "showPlaying", Toast.LENGTH_SHORT).show();
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showSeek() {
        Toast.makeText(this, "seek", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showPlayError(int code,String error) {
        Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showValidateFailed(int i, String s) {
    }

}
